﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace AGKWrapperAid
{
    public enum TokenType
    {
        StaticQualifier,
        ParameterStart,
        ParameterEnd,
        ParameterSep,
        ParameterEqual,
        Literal,
        LineEnd,
        Number
    }

    public class Token
    {
        public string Symbol;
        public TokenType Type;
        public Token(string symbol, TokenType type)
        {
            Symbol = symbol;
            Type = type;
        }
    }
    
    public class Tokenizer
    {
        public List<Token> Tokenize(string input)
        {
            var tokens = new List<Token>();
            char c = input[0];
            int i = 0;

            // be lazy and don't deal with const
            input = input.Replace("const", "");

            while (i < input.Length)
            {
                c = input[i++];
                if (char.IsWhiteSpace(c))
                {
                    continue;
                }
                else if (char.IsLetter(c) || c == '*' || c == '&')
                {
                    string t = "";
                    while (char.IsLetterOrDigit(c) || c == '*' || c == '&')
                    {
                        t += c; c = input[i++];
                    }
                    i--;
                    tokens.Add(new Token(t, TokenType.Literal));
                }
                else if (char.IsNumber(c) || c=='-')
                {
                    string t = "";
                    while (char.IsNumber(c) || c == '-')
                    {
                        t += c; c = input[i++];
                    }
                    tokens.Add(new Token(t, TokenType.Number));
                }
                else if (c == '(')
                {
                    tokens.Add(new Token(null, TokenType.ParameterStart));
                }
                else if (c == ')')
                {
                    tokens.Add(new Token(null, TokenType.ParameterEnd));
                }
                else if (c == ',')
                {
                    tokens.Add(new Token(null, TokenType.ParameterSep));
                }
                else if (c == '=')
                {
                    tokens.Add(new Token(null, TokenType.ParameterEqual));
                }
                else if (c == ';')
                    tokens.Add(new Token(null, TokenType.LineEnd));
                else
                    throw new Exception(string.Format("Unexpected character: {0}", c));
            }

            return tokens;
        }
    }
}
